<?php
session_start();
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/database.php');
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/functions.php');
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/class.admin.php');
$admin = new admin();
$admin->canView($_SERVER['PHP_SELF']);


$data= new database();
$data->db(DATABASE)->collection('media');
$data->criteria(array('path'=>array('$exists'=>true)))->sort(array('date'=>-1));
$rows=$data->findNl();
echo('<h2 class="form-heading">Available images</h2>');
echo('<div style="height:230px;overflow:auto;">');
echo('<ul>');
foreach($rows['results'] as $row) {
?>
<li style="float:left;padding:2px;border:1px solid #ddd;cursor:pointer;margin-right:2px;" class="mm-edia"><a href="#" class="insert-image" style="display:block;">
<input type="hidden" class="largesettings" value="<?php echo($row['large']['path']); ?>,<?php echo($row['large']['width']); ?>,<?php echo($row['large']['height']); ?>,<?php echo((string)'image_'.$row['_id']); ?>,<?php echo($row['path']); ?>,<?php echo((string)$row['_id']); ?>" />
<input type="hidden" class="thumbsettings" value="<?php echo($row['thumb']['path']); ?>,<?php echo($row['thumb']['width']); ?>,<?php echo($row['thumb']['height']); ?>,<?php echo((string)'image_'.$row['_id']); ?>,<?php echo($row['path']); ?>,<?php echo((string)$row['_id']); ?>" />
<input type="hidden" class="settings" value="<?php echo($row['path']); ?>,<?php echo($row['dimensions']['width']); ?>,<?php echo($row['dimensions']['height']); ?>,<?php echo((string)'image_'.$row['_id']); ?>,<?php echo($row['path']); ?>,<?php echo((string)$row['_id']); ?>" />
<img src="<?php echo($row['thumb']['path']); ?>" class="tip" title="Click to add - <?php echo($row['large']['width'].'px X '.$row['large']['height'].'px'); ?>" /></a></li>

<?php
}
echo('</ul>');
echo('</div>');
?>
<div style="height:28px;border-top:1px solid #c4e4fa;padding-top:3px;">
<table width="100%">
<tr valign="top">
<td width="3%"><strong>Alt:</strong></td>
<td width="42%"><input type="text" id="insert-media-alt" class="form-field" style="padding-right:0px;margin-right:1em;" /></td>
<td width="9%"><strong style="margin-left:1em;">Float:</strong></td>
<td width="15%"><select id="insert-media-float" class="form-field" ><option value="">No Float</option><option value="float-left">Float Left</option><option value="float-right">Float Right</option></select></td>
<td width="5%"><strong>Size:</strong></td>
<td width="15%"><select id="insert-media-size" class="form-field" style="padding-right:0px;margin-right:1em;">
<option value="large">Large</option>
<option value="thumb">Thumb</option>
<option value="">Original</option>

</select></td>
<td width="6%"><input type="submit" value="Delete" id="insert-media-delete" /></td>
<td width="5%"><input type="submit" value="Insert" style="float:right;" id="insert-media-button" /></td>
</tr>
</table>


</div>
<script>
$(".insert-image").click(function(event){
	event.preventDefault();
	$('.the-selected-media').removeClass('the-selected-media');
	$(this).addClass('the-selected-media');
});
	$("#insert-media-delete").click(function(event){
	var element=$(".the-selected-media");
	var settings=element.find('input.settings').val();
	var s=settings.split(",");
	var id=s[(s.length-1)];
		if(confirm('Are you sure you want to delete this image')) {
			$.post("/admin/resources/php/requests/actions/delete-image.php",{id:id},function(data){
			
				$("#response").html(data);
				element.parent('.mm-edia').remove();
				top.load_media_gallery("#images-gallery","page_content");

			});
		
		}
	});
	
	
 $('#insert-media-button').click(function(event){
	event.preventDefault();
 	var element=$(".the-selected-media");
	var ss=$("#insert-media-size").val();
	var settings=element.find('input.'+ss+'settings').val();
	var s=settings.split(",");
	var f=$("#insert-media-float").val();
	var a=$("#insert-media-alt").val();
	f+=' thumb';
	var c=$.trim(f);
	 $('#<?php echo($_POST['oid']); ?>').wysiwyg('insertImage', s[0], { 'class': c, 'className': c, 'width' : s[1], 'height' : s[2], 'alt' : a, 'id': s[3] },s[4]);
$(this).parents('.dialogue').find('.dialogue-close').click();
 });
</script>
